package com.asion.qkan.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.asion.qkan.R;
import com.asion.qkan.adapters.MusicAdapter;
import com.asion.qkan.api.AppConstant;
import com.asion.qkan.entry.MusicBean;
import com.asion.qkan.ui.contract.MusicContract;
import com.asion.qkan.ui.model.MusicModel;
import com.asion.qkan.ui.presenter.MusicPresenter;
import com.asion.qkan.ui.util.GridSpacingItemDecoration;
import com.asion.qkan.ui.view.BaseActivity;

import butterknife.BindView;

public class MusicActivity extends BaseActivity<MusicPresenter, MusicModel> implements MusicContract.View, MusicContract.OnMusicItemClickListener {

    @BindView(R.id.music_toolbar)
    Toolbar musicToolbar;
    @BindView(R.id.music_recycler)
    RecyclerView musicRecycler;
    private MusicAdapter musicAdapter;

    public static void startAction(Activity activity) {
        Intent intent = new Intent(activity, MusicActivity.class);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                R.anim.fade_out);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_music;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
        mPresenter.setMusicDatas(AppConstant.MUSICAPI);
    }

    @Override
    public void initView() {
        setSupportActionBar(musicToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        musicRecycler.setItemAnimator(new DefaultItemAnimator());
        musicRecycler.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
        musicRecycler.addItemDecoration(new GridSpacingItemDecoration(3,0,true));
        musicAdapter = new MusicAdapter(this);
        musicRecycler.setAdapter(musicAdapter);

        musicAdapter.setOnMusicItemClickListener(this);

        startProgressDialog();
    }

    @Override
    public void showDataError(Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void showDataSuccess(MusicBean.ResultBean bean) {
        musicAdapter.addAll(bean.getTracks());
        stopProgressDialog();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onMusicItemClick(int position, MusicBean.ResultBean.TracksBean resultBean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("trackbean",resultBean);
        PlayActivity.startAction(this,bundle);
    }


    @Override
    public void onBackPressed() {
        finish();
    }
}
